package cn.bat.slsystem.control;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.bat.slsystem.pojo.AuGbook;
import cn.bat.slsystem.pojo.AuNotice;
import cn.bat.slsystem.pojo.AuUser;
import cn.bat.slsystem.pojo.Msg;
import cn.bat.slsystem.service.GbookService;
import cn.bat.slsystem.tools.Constants;

@Controller
@RequestMapping(value="/gbook")
public class GbookController {
	@Autowired
	private GbookService gbookService;
	
	/**
	 * 展示界面
	 * @return
	 */
	@RequestMapping(value="/gbooklist.html")
	public String msglist(HttpSession session){
		List<String> list = (List) session.getAttribute(Constants.lanjie);
		for (String path : list) {
			if(path.equals("gbook/gbooklist.html")){
				return "gbooklist";
			}
		}
		return "login";
	}
	
	/**
	 * 获得数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/show.html")
	@ResponseBody
	public Object serch(@RequestParam(required=false,defaultValue="1") int page,@RequestParam(required=false,defaultValue="5")int limit
			,HttpSession session){
		AuUser auUser =(AuUser) session.getAttribute(Constants.USER_SESSION);
		String username = auUser.getLogincode();
		if(auUser.getRoleid()==1){
			username=null;
		}
		PageHelper.startPage(page, limit);
		List<AuGbook> list =gbookService.selectByPrimaryKey(username);
		System.out.println("=================="+list);
		PageInfo<AuGbook> pageinfo=new PageInfo<>(list);
		return pageinfo;
	}
	/*提交留言*/
	@RequestMapping(value="/add.html")
	public String addgbook(AuGbook gbook,HttpSession session){
		AuUser auUser =(AuUser) session.getAttribute(Constants.USER_SESSION);
		String username = auUser.getLogincode();
		gbook.setGbookuser(username);
		gbook.setGbooktime(new Date());
		gbook.setIsreply((long) 0);
		gbookService.insertSelective(gbook);
		return "redirect:gbooklist.html";
	}
	
	/*回复留言*/
	@RequestMapping(value="/update.html")
	public Object updaterole(AuGbook gbook){
		gbook.setReplytime(new Date());
		if(null !=gbook.getReplycontent()|| !"".equals(gbook.getReplycontent())){
			gbook.setIsreply((long) 1);
		}
		int count = gbookService.updateByPrimaryKeySelective(gbook);
		if(count>0){
			return "redirect:gbooklist.html";
		}else{
			return "redirect:gbooklist.html";
		}
	}
	
	/*根据id删除信息*/
	@RequestMapping(value="/del.html")
	@ResponseBody
	public String del(Integer id){
		if(gbookService.deleteByPrimaryKey(id)==1){
			return "ok";
		}
		return "no";
	}
	
}
